﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using RRRSRoguelike.Entities;
using RRRSRoguelike.Entities.Props;
using RRRSRoguelike.Enums;

namespace RRRSRoguelike.Managers
{
	/// <summary>
	/// Manages properties of tiles including effects and weapons.
	/// </summary>
	class PropManager
	{

		#region variables
		private IList<Tile> props;


		public Tile LookTile;
		public Fireball Fireball;
		#endregion

		public IList<Tile> Props
		{
			get { return props; }
		}


		#region methods
		public PropManager()
		{
			props = new List<Tile>();
			Fireball = new Fireball(new Point(), Direction.N);
			LookTile = new Tile();
		}

		public void AddStairwell(Point position)
		{
			props.Add(new Stairwell(position, StairDirection.Up));
			PropsToTiles();
		}

		public void ClearProps()
		{
			props.Clear();
		}

		public void AddProps()
		{
			for (var i = 0; i < Constants.NumberOfSwords; i++)
			{
				var index = GameManager.Instance.Dungeon.DungeonBuilderRandom.Next(
					0, GameManager.Instance.Dungeon.Data.Props.Count);

				var weapon = new Weapon(GameManager.Instance.DungeonLevel.GetValidRandomPoint(true),
									   GameManager.Instance.Dungeon.Data.Props.ElementAt(index).Value);
				props.Add(weapon);
			}

			AddStairwell(GameManager.Instance.DungeonLevel.GetValidRandomPoint(true));

		}

		//FIXME: this belongs in a ruleset
		public Weapon GetBestWeapon(Point location)
		{
			var weapon = props.OfType<Weapon>().Where(s => s.Position == location).OrderBy(s => s.UsesRemaining).Last();
			return weapon;
		}

		public void Add(Tile prop)
		{
			props.Add(prop);
		}

		public void Remove(Tile prop)
		{
			props.Remove(prop);
		}

		public void PropsToTiles()
		{
			props.ToList().ForEach(s => GameManager.Instance.DungeonLevel.AddTile(s, Layers.Prop));
			if (Fireball.Active) GameManager.Instance.DungeonLevel.AddTile(Fireball, Layers.Effect);
			if (LookTile.Active) GameManager.Instance.DungeonLevel.AddTile(LookTile, Layers.Effect);
			GameManager.Instance.DungeonLevel.walls.ToList().ForEach(
				w => GameManager.Instance.DungeonLevel.AddTile(w, Layers.Prop));
		}
		#endregion

	}
}
